/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IF6501;

import java.io.*;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author ariefiandinugraha
 */
public class Praproses {
    private File IgnoreFile;
    
    Set<String> ignoreWords;
    

    public File getIgnoreFile() {
        return IgnoreFile;
    }

    public void setIgnoreFile(File IgnoreFile) {
        this.IgnoreFile = IgnoreFile;
    }
    
    
    public Praproses(){
     ignoreWords = new HashSet<String>();
    }
    
    
    private void ignoreFile(File ignoreFile){
        try{
            Scanner ignoreScanner = new Scanner(ignoreFile);
            ignoreScanner.useDelimiter("[^A-Za-z]+");
            while(ignoreScanner.hasNext()){
                ignoreWords.add(ignoreScanner.next());
            }
        }
        catch(FileNotFoundException ex) {
            Logger.getLogger(Praproses.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void removeStopWord(File inputFile){
        
        //get stopwords dari ignore file
        ignoreFile(getIgnoreFile());
        try{
            FileWriter fw = new FileWriter(inputFile.getName()+"_praproses");
            BufferedWriter bw = new BufferedWriter(fw);
            Scanner lineScanner = new Scanner(inputFile);
            //baca file input perbaris
            while(lineScanner.hasNext()){
                String res = "";
                String instance = lineScanner.nextLine();
                Scanner wordScanner = new Scanner(instance);
                wordScanner.useDelimiter("[^A-Za-z]+");
                //cek tiap kata dari baris tsb
                while(wordScanner.hasNext()){
                    String word = wordScanner.next();
                    if(!ignoreWords.contains(word)){
                        res += word+" ";
                    }
                }
                System.out.println("out :" + res);
                //cek apabila pjg kata jika lebih dari 1 masukan ke file
                String[] cek = res.split("\\s");
                if(cek.length>1)
                    bw.write(res+"\n");
                wordScanner.close();
            }
            
        bw.close();
        lineScanner.close();
        }
        catch (FileNotFoundException ex) {
            Logger.getLogger(Praproses.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch(IOException ex){
            Logger.getLogger(Praproses.class.getName()).log(Level.SEVERE, null, ex);
        }
     
    }
}
